/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.property.vilageinfo.service;

import java.util.List;

import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.OfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.property.vilageinfo.entity.PropertyVillageinfo;
import com.jeeplus.modules.property.vilageinfo.mapper.PropertyVillageinfoMapper;

/**
 * 小区信息Service
 *
 * @author 陈龙
 * @version 2019-01-04
 */
@Service
@Transactional(readOnly = true)
public class PropertyVillageinfoService extends CrudService<PropertyVillageinfoMapper, PropertyVillageinfo> {
    @Autowired
    private OfficeService officeService;
    public PropertyVillageinfo get(String id) {
        return super.get(id);
    }

    public List<PropertyVillageinfo> findList(PropertyVillageinfo propertyVillageinfo) {
        return super.findList(propertyVillageinfo);
    }

    public Page<PropertyVillageinfo> findPage(Page<PropertyVillageinfo> page, PropertyVillageinfo propertyVillageinfo) {
        return super.findPage(page, propertyVillageinfo);
    }

    @Transactional(readOnly = false)

    public void save(PropertyVillageinfo propertyVillageinfo) {
        boolean bj = false;
        if (propertyVillageinfo.getIsNewRecord()) {
            bj = true;
        }
        //保存小区信息后同时保存小区进入机构信息，用于用户登录机构管理
        super.save(propertyVillageinfo);
        if (bj) {
            //保存小区信息后同时保存小区进入机构信息，用于用户登录机构管理
            Office o = new Office();
            Office sjo = officeService.getByCode("1000001");//查询上级机构，默认
            o.setParent(sjo);
            o.setArea(sjo.getArea());
            o.setCode(propertyVillageinfo.getId());
            o.setName(propertyVillageinfo.getVillname());
            o.setGrade("2");//机构级别 默认为2
            o.setUseable("1");
            officeService.save(o);
        }
    }

    @Transactional(readOnly = false)
    public void delete(PropertyVillageinfo propertyVillageinfo) {
        super.delete(propertyVillageinfo);
        Office o = officeService.getByCode(propertyVillageinfo.getId());
        if (o != null) {
            officeService.delete(o);
        }
    }

}